class LecturesController < ApplicationController
  # GET /lectures
  # GET /lectures.json
  helper_method :getAverage
  helper_method :getAverageProject

  def index
    @lectures = Lecture.all
    @lectures = search(params[:search]) 
    @lectures = sort(params[:sort])
    

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @lectures }
    end
  end

  # GET /lectures/1
  # GET /lectures/1.json
  def show
    @lecture = Lecture.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @lecture }
    end
  end

  # GET /lectures/new
  # GET /lectures/new.json
  def new
    @lecture = Lecture.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @lecture }
    end
  end

  # GET /lectures/1/edit
  def edit
    @lecture = Lecture.find(params[:id])
  end

  # POST /lectures
  # POST /lectures.json
  def create
    @lecture = Lecture.new(params[:lecture])

    respond_to do |format|
      if @lecture.save
        if params[:first] != "" && params[:last] != "" && params[:dep] != "" && params[:prof] == ""
          @proff = Professor.new
          @proff.first_name = params[:first]
          @proff.last_name = params[:last]
          @proff.department = params[:dep]
          @proff.save

          @glectures = Giveslecture.new
          @glectures.professor_id = @proff.id
          @glectures.lecture_id = @lecture.id
          @glectures.save
        else
          @glectures = Giveslecture.new
          @glectures.professor_id = params[:prof]
          @glectures.lecture_id = @lecture.id
          @glectures.save
        end
        #@proff = Professor.find(params[:prof_id])
        format.html { redirect_to @lecture, notice: 'Lecture was successfully created' }
        format.json { render json: @lecture, status: :created, location: @lecture }
      else
        format.html { render action: "new" }
        format.json { render json: @lecture.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /lectures/1
  # PUT /lectures/1.json
  def update
    @lecture = Lecture.find(params[:id])

    respond_to do |format|
      if @lecture.update_attributes(params[:lecture])
        format.html { redirect_to @lecture, notice: 'Lecture was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @lecture.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /lectures/1
  # DELETE /lectures/1.json
  def destroy
    @lecture = Lecture.find(params[:id])
    @lecture.destroy

    respond_to do |format|
      format.html { redirect_to lectures_url }
      format.json { head :no_content }
    end
  end

  def getAverage(lecture,attr)
    if lecture.lecture_ratings.blank? then
      return 0
    else
      avgRating = 0
      avgDifficulty = 0
      avgPrDifficulty = 0
      avgWork = 0
      ratings = 0

      lecture.lecture_ratings.each do |rating|
        avgRating += rating.rating
        avgDifficulty += rating.difficulty
        avgWork += rating.work_load
        ratings += 1
      end

      if ratings == 0 then
        avgRating, avgDifficulty, avgWork = 0
      else
        avgRating /= ratings
        avgDifficulty /= ratings
        avgWork /= ratings
      end

      avgRating = Integer(avgRating)
      avgDifficulty = Integer(avgDifficulty)
      avgWork = Integer(avgWork)

      if attr == "rating" then
        return avgRating
      elsif attr == "diff" then
        return avgDifficulty
      else
        return avgWork
      end
    end
  end

  def getAverageProject(lecture,attr)
    if ExerciseRating.find_all_by_lecture_id(lecture.id).blank? then
      return 0
    else
      avgRating = 0;
      avgDifficulty = 0
      avgWork = 0
      ratings = 0

      ExerciseRating.find_all_by_lecture_id(lecture.id).each do |rating|
        avgRating += rating.rating;
        avgWork += rating.work_load
        avgDifficulty += rating.difficulty
        ratings += 1
      end

      if ratings == 0 then
        avgRating, avgDifficulty, avgWork = 0
      else
        avgRating /= ratings
        avgDifficulty /= ratings
        avgWork /= ratings
      end

      if attr == "work" then
        return avgWork
      elsif attr == "rating" then
        return avgRating
      else
        return avgDifficulty
      end
    end
  end
  
  def sort(params)
    if(params=="title")
      return @lectures.sort! { |a,b| a.title <=> b.title}

    else if(params=="rating")
      return @lectures.sort! { |a,b| getAverage(b,"rating") <=> getAverage(a,"rating")}
    else
      return @lectures
    end
    end
  end
  
  def search(search)
          if search
            return Lecture.find(:all, :conditions => ['Title LIKE ?', "%#{search}%"])
          else
              return Lecture.find(:all)
          end
  end
  
end
